package com.sghy.xcxservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.sghy.xcxservice.entity.BgbClockIn;
import com.sghy.xcxservice.service.BgbClockInService;
import com.sghy.xcxservice.mapper.BgbClockInMapper;
import com.sghy.xcxservice.service.BgbOrgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

/**
* @author pc
* @description 针对表【bgb_clock_in】的数据库操作Service实现
* @createDate 2025-05-30 16:11:31
*/
@Service
public class BgbClockInServiceImpl extends ServiceImpl<BgbClockInMapper, BgbClockIn>
    implements BgbClockInService{
    @Autowired
    private BgbOrgUserService bgbOrgUserService;
    
    @Override
    public void location(String uid, double latitude, double longitude, String description, String type, String photos) {
        BgbClockIn bgbClockIn = new BgbClockIn();
        bgbClockIn.setUId(uid);
        bgbClockIn.setLatitude(latitude);
        bgbClockIn.setLongitude(longitude);
        bgbClockIn.setDescription(description);
        bgbClockIn.setType(type);
        if (Objects.equals(type, "photo")){
            bgbClockIn.setPhotos(JSON.parseArray(photos));
        }
        baseMapper.insert(bgbClockIn);
    }
    
    /**
     * 查询我的今天的打开记录，从今天00：00开始
     * @param uid
     * @return
     */
    @Override
    public List<BgbClockIn> getMyTodayClockIn(String uid) {
        return this.lambdaQuery()
                .eq(BgbClockIn::getUId, uid)
                .ge(BgbClockIn::getTime, java.time.LocalDate.now().atStartOfDay())
                .orderByDesc(BgbClockIn::getTime)
                .list();
    }
    /**
     * 查询我的历史打卡记录，需要分页查询及支持选择那一天查询
     */
    @Override
    public Object getMyHistoryClockIn(String uid, int page, int size, String date) {
        // 这里需要实现分页逻辑，假设使用 Page 对象进行分页
        PageHelper.startPage(page,size);
        
        // 2. 构建独立Wrapper
        LambdaQueryWrapper<BgbClockIn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BgbClockIn::getUId, uid)
                .like(StringUtils.isNotBlank(date), BgbClockIn::getTime, date)
                .orderByDesc(BgbClockIn::getTime);
        // 3. 执行查询
        return  baseMapper.selectList(wrapper);
    }
    /**
     * 查询所有人的打卡记录列表，按照日期查询，支持分页查询，日期必填
     */
    @Override
    public Object getAllClockIn(String date, int page, int size) {
        // 这里需要实现分页逻辑，假设使用 Page 对象进行分页
        PageHelper.startPage(page,size);
        // 2. 构建独立Wrapper
        LambdaQueryWrapper<BgbClockIn> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(BgbClockIn::getTime, date)
                .orderByDesc(BgbClockIn::getTime);
        List<BgbClockIn> list = baseMapper.selectList(wrapper);
        for (BgbClockIn bgbClockIn : list) {
            bgbClockIn.setUser(bgbOrgUserService.getUserInfo(bgbClockIn.getUId()));
        }
        // 3. 执行查询
        return list;
    }
    
    @Override
    public Object getTodayClockInUser() {
        LambdaQueryWrapper<BgbClockIn> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(BgbClockIn::getTime, java.time.LocalDate.now().atStartOfDay())
                .orderByDesc(BgbClockIn::getTime);
        List<BgbClockIn> list = baseMapper.selectList(wrapper);
        
        // 使用 Map 来存储每个用户的最后一次打卡记录
        Map<String, BgbClockIn> lastClockInMap = new LinkedHashMap<>();
        for (BgbClockIn bgbClockIn : list) {
            if (!lastClockInMap.containsKey(bgbClockIn.getUId())) {
                bgbClockIn.setUser(bgbOrgUserService.getUserInfo(bgbClockIn.getUId()));
                lastClockInMap.put(bgbClockIn.getUId(), bgbClockIn);
            }
        }
        
        // 返回每个用户的最后一次打卡记录
        return new ArrayList<>(lastClockInMap.values());
        
    }
}




